package cn.echoedu.specifics;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.AClass;
import cn.echoedu.entity.Degress;
import cn.echoedu.entity.Department;
import cn.echoedu.entity.Image;
import cn.echoedu.entity.Job;
import cn.echoedu.entity.PoliticalStatus;
import cn.echoedu.entity.Professional;
import cn.echoedu.entity.School;
import cn.echoedu.entity.SchoolCalendar;
import cn.echoedu.entity.Student;
import cn.echoedu.util.L;

public class SpecStudent extends BaseSpec{
	private final static String T="SpecStudent-------->";
	/**
	 * id need to be inserted
	 * @param con
	 * @param tableName 
	 * @param currentPojo the info needed to be inserted
	 * @return <T>
	 * @throws SQLException
	 */
	public <T> PreparedStatement getPreparedInsert(Connection con,
			TableName tableName,T currentPojo)throws SQLException{
		L.gpi(T);
		Student currentstudent=(Student)currentPojo;
		PreparedStatement preparedstatement=con.prepareStatement("insert into "
				+tableName+" (studentnumber,name,realname,sex,birthday," +
				"entrancetime,password,examineenumber," +
				"qq,email,baidunumber,sinanumber,wechatnumber," +
				"jobcode,classcode,imageid,degresscode,politicalstatuscode)" +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		preparedstatement.setString(1,currentstudent.getStudentnumber());
		preparedstatement.setString(2,currentstudent.getName());
		preparedstatement.setString(3,currentstudent.getRealname());
		preparedstatement.setString(4,currentstudent.getSex());
		preparedstatement.setDate(5,(Date) currentstudent.getBirtyday());
		preparedstatement.setDate(6,(Date) currentstudent.getEntrancetime());
		preparedstatement.setString(7,currentstudent.getPassword());
		preparedstatement.setString(8,currentstudent.getExamineenumber());
		preparedstatement.setString(9,currentstudent.getQq());
		preparedstatement.setString(10,currentstudent.getEmail());
		preparedstatement.setString(11,currentstudent.getBaidunumber());
		preparedstatement.setString(12,currentstudent.getSinanumber());
		preparedstatement.setString(13,currentstudent.getWechatnumber());
		if(currentstudent.getJob()!=null){
			preparedstatement.setInt(14,currentstudent.getJob().getJobcode());
		}else{
			preparedstatement.setNull(14,Types.INTEGER);
		}
		if(currentstudent.getAclass()!=null){
			preparedstatement.setInt(15,currentstudent.getAclass().getClasscode());
		}else{
			preparedstatement.setNull(15,Types.INTEGER);
		}
		if(currentstudent.getImage()!=null){
			preparedstatement.setInt(16,currentstudent.getImage().getImageid());
		}
		else{
			preparedstatement.setNull(16,Types.INTEGER);
		}
		if(currentstudent.getDegress()!=null){
			preparedstatement.setInt(17,currentstudent.getDegress().getDegressid());
		}else{
			preparedstatement.setNull(17,Types.INTEGER);
		}
		if(currentstudent.getPoliticalstatus()!=null){
			preparedstatement.setInt(18,currentstudent.getPoliticalstatus().getPoliticalstatuscode());
		}else{
			preparedstatement.setNull(18,Types.INTEGER);
		}
		return preparedstatement;

	}
	/**
	 * id doesn't need to be inserted
	 * @param con
	 * @param tableName
	 * @param currentPojo
	 * @return
	 * @throws SQLException
	 */
	public <T> PreparedStatement getPreparedInsertRunning(Connection con,
			TableName tableName,T currentPojo)throws SQLException{
		final String M="getPreparedInsertRunning*************>>";
		L.gpir(T+M);
		Student currentstudent=(Student)currentPojo;
		String sql="insert into "+tableName+"(name,realname,sex,birthday,entrancetime," +
				"password,examineenumber,qq,email,baidunumber,sinaumber," +
				"wechatnumber,jobcode,classcode,imageid,degresscode,politicalstatuscode)" +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement preparedstatement=con.prepareStatement(sql);
		preparedstatement.setString(1,currentstudent.getName());
		preparedstatement.setString(2,currentstudent.getRealname());
		preparedstatement.setString(3,currentstudent.getSex());
		preparedstatement.setDate(4,(Date) currentstudent.getBirtyday());
		preparedstatement.setDate(5,(Date) currentstudent.getEntrancetime());
		preparedstatement.setString(6,currentstudent.getPassword());
		preparedstatement.setString(7,currentstudent.getExamineenumber());
		preparedstatement.setString(8,currentstudent.getQq());
		preparedstatement.setString(9,currentstudent.getEmail());
		preparedstatement.setString(10,currentstudent.getBaidunumber());
		preparedstatement.setString(11,currentstudent.getSinanumber());
		preparedstatement.setString(12,currentstudent.getWechatnumber());
		preparedstatement.setInt(13,currentstudent.getJob().getJobcode());
		preparedstatement.setInt(14,currentstudent.getAclass().getClasscode());
		preparedstatement.setInt(15,currentstudent.getImage().getImageid());
		preparedstatement.setInt(16,currentstudent.getDegress().getDegressid());
		preparedstatement.setInt(17,currentstudent.getPoliticalstatus().getPoliticalstatuscode());
		return preparedstatement;

	}
	 
	public Student getPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		L.gp(T);
		Student currentstudent=new Student();
		currentstudent.setStudentnumber(rs.getString(1));
		currentstudent.setName(rs.getString(2));
		currentstudent.setRealname(rs.getString(3));
		currentstudent.setSex(rs.getString(4));
		currentstudent.setBirtyday(rs.getDate(5));
		currentstudent.setEntrancetime(rs.getDate(6));
		currentstudent.setPassword(rs.getString(7));
		currentstudent.setExamineenumber(rs.getString(8));
		currentstudent.setQq(rs.getString(9));
		currentstudent.setEmail(rs.getString(10));
		currentstudent.setBaidunumber(rs.getString(11));
		currentstudent.setSinanumber(rs.getString(12));
		currentstudent.setWechatnumber(rs.getString(13));
		Job job=new Job();
		job.setJobcode(rs.getInt(14));
		currentstudent.setJob(job);
		AClass aclass=new AClass();
		aclass.setClasscode(rs.getInt(15));
		currentstudent.setAclass(aclass);
		Image image=new Image();
		image.setImageid(rs.getInt(16));
		currentstudent.setImage(image);
		Degress degress=new Degress();
		degress.setDegressid(rs.getInt(17));
		currentstudent.setDegress(degress);
		PoliticalStatus politicalstatus=new PoliticalStatus();
		politicalstatus.setPoliticalstatuscode(rs.getInt(18));
		currentstudent.setPoliticalstatus(politicalstatus);
		return currentstudent;
	}
	public <T> PreparedStatement getPreparedUpdate(Connection con,
			TableName tableName,T oldPojo,T currentPojo)throws SQLException{
		Student currentstudent=(Student)currentPojo;
		Student oldstudent=(Student)oldPojo;
		String sql="update "+tableName+"set name=?,realname=?,sex=?,birthday=?" +
				"entrancetime=?,password=?,examineenumber=?,qq=?,email=?,baidunumber=?," +
				"sinanumber=?,wechatnumber=?,jobcode=?,classcode=?,imageid=?," +
				"degresscode=?,politicalstatuscode=? where studentnumber=?";
		PreparedStatement preparedstatement=con.prepareStatement(sql);
		preparedstatement.setString(1,currentstudent.getName());
		preparedstatement.setString(2,currentstudent.getRealname());
		preparedstatement.setString(3,currentstudent.getSex());
		preparedstatement.setDate(4,(Date) currentstudent.getBirtyday());
		preparedstatement.setDate(5,(Date) currentstudent.getEntrancetime());
		preparedstatement.setString(6,currentstudent.getPassword());
		preparedstatement.setString(7,currentstudent.getExamineenumber());
		preparedstatement.setString(8,currentstudent.getQq());
		preparedstatement.setString(9,currentstudent.getEmail());
		preparedstatement.setString(10,currentstudent.getBaidunumber());
		preparedstatement.setString(11,currentstudent.getSinanumber());
		preparedstatement.setString(12,currentstudent.getWechatnumber());
		preparedstatement.setInt(13,currentstudent.getJob().getJobcode());
		preparedstatement.setInt(14,currentstudent.getAclass().getClasscode());
		preparedstatement.setInt(15,currentstudent.getImage().getImageid());
		preparedstatement.setInt(16,currentstudent.getDegress().getDegressid());
		preparedstatement.setInt(17,currentstudent.getPoliticalstatus().getPoliticalstatuscode());
		preparedstatement.setString(18,currentstudent.getStudentnumber());
		return preparedstatement;
	}
	@Override
	public Object getLeftJoinPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object getPojoBySql(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub

		Student currentstudent=new Student();
		currentstudent.setStudentnumber(rs.getString("studentnumber"));
		currentstudent.setName(rs.getString("name"));
		currentstudent.setRealname(rs.getString("realname"));
		currentstudent.setSex(rs.getString("sex"));
		currentstudent.setBirtyday(rs.getDate("birthday"));
		currentstudent.setEntrancetime(rs.getDate("entrancetime"));
		currentstudent.setPassword(rs.getString("password"));
		currentstudent.setExamineenumber(rs.getString("examineenumber"));
		currentstudent.setQq(rs.getString("qq"));
		currentstudent.setEmail(rs.getString("email"));
		currentstudent.setBaidunumber(rs.getString("baidunumber"));
		currentstudent.setSinanumber(rs.getString("sinanumber"));
		currentstudent.setWechatnumber(rs.getString("wechatnumber"));
		Job job=new Job();
		try{
			job.setJobcode(rs.getInt("jobcode"));
			job.setJobname(rs.getString("jobname"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		currentstudent.setJob(job);
		AClass aclass=new AClass();
		try{
			aclass.setClasscode(rs.getInt("classcode"));
			aclass.setClassname(rs.getString("classname"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		Professional professional = new Professional();
		try{
			professional.setProfessionalcode(rs.getInt("professionalcode"));
			professional.setProfessionalname(rs.getString("professional.name"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		Department department = new Department();
		try{
			department.setDepartmentcode(rs.getInt("departmentcode"));
			department.setDepartname(rs.getString("departname"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		School school =new School();
		try{
			school.setSchoolcode(rs.getInt("schoolcode"));
			school.setSchoolname(rs.getString("school.name"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		department.setSchool(school);
		professional.setDepartment(department);
		aclass.setProfessional(professional);
		currentstudent.setAclass(aclass);
		Image image=new Image();
		try{
			image.setImageid(rs.getInt("imageid"));
			image.setImagename(rs.getString("imagename"));
			image.setImagesrc(rs.getString("imagesrc"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		currentstudent.setImage(image);
		Degress degress=new Degress();
		try{
			degress.setDegressid(rs.getInt("degressid"));
			degress.setDegressname(rs.getString("degressname"));
			degress.setContent(rs.getString("content"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		currentstudent.setDegress(degress);
		PoliticalStatus politicalstatus=new PoliticalStatus();
		try{
			politicalstatus.setPoliticalstatuscode(rs.getInt("politicalstatuscode"));
			politicalstatus.setPoliticalstatusname(rs.getString("politicalstatusname"));
			politicalstatus.setDescription(rs.getString("description"));
		}catch(SQLException e){
			e.printStackTrace();
		}
		currentstudent.setPoliticalstatus(politicalstatus);
		L.p("~!@#$%^&*()_~!@#$%^&*()_");
		return currentstudent;

	}
	@Override
	public <T> String getPreparedInsertGetPrimaryKeySql(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public <T> PreparedStatement getPreparedListInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public <T> PreparedStatement getPreparedListInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}


}
